# Sustainable Office Designer
# by Fabian Ritter <Fabian.Ritter@live.de>, 2012

require("SOD/sketchup/sketchup")

module SOD
  
  class FootprintInstanceObserver < Sketchup::InstanceObserver
    
    def onOpen
      # nothing to do here, yet...
    end
    
    def onClose( instance )
      if instance.valid?
        updateDimension( instance )
        Plugin.dialogManager.update_all
        puts "instance updated" if $debug
      end
    end
    
    def updateDimension( group )
      
      len = group.name.index("_")
      entName = group.name[0...len]
      UI.messagebox "Active Bar: " + entName.to_s if $debug
      
      bar = Plugin.model.bars.find { |b| b.name == entName }
      
      bar.sketchInfo.update_from_group( group )
      
      bar.check_if_intersect?
      
    end
    
  end
  
  class FootprintFaceObserver < Sketchup::EntityObserver
    
    def onChangeEntity(entity)
      self.checkOrientation(entity)
      
      Plugin.dialogManager.update_all
      
      puts "Face changed" if $debug
    end
    
    def onEraseEntity( entity )
      active_path = Sketchup.active_model.active_path
      if not active_path.nil?
        barName = active_path.first.name
        Plugin.model.removeBar( barName )
      end
      
      puts "Footprint deleted as Face" if $debug
      
    end
    
    def checkOrientation(face)
      if face.normal.z < 0
        face.reverse! 
        puts "reversed"
      end
    end
    
  end
  
  class FootprintGroupObserver < Sketchup::EntityObserver
    
    
    def onChangeEntity( entity )
      
      if entity.valid?
      
        len = entity.name.index("_")
        entName = entity.name[0...len]
        UI.messagebox "Active Bar: " + entName.to_s if $debug
        
        bar = Plugin.model.bars.find { |b| b.name == entName }
        
        bar.sketchInfo.update_from_group( entity )
        
        bar.check_if_intersect?
        
        Plugin.dialogManager.update_all
        
      end
      
      puts "group changed" if $debug
    end

    def onEraseEntity( entity )
      
      Plugin.model.removeBar( entity.name )
      
      puts "Footprint deleted as Group" if $debug
    end
    
  end
 
end
